<template>
    <AdminSkeleton :headerIsHeight="true">
        <template #header>
            <div class="params-form">
                <el-form label-width="auto" :model="paramsForm" :rules="pfRules" ref="pfFormRef">
                    <div class="one-box">
                        <el-form-item label="年份:" size="small" prop="planYear" >
                            <el-config-provider>
                                <el-select v-model="paramsForm.planYear" placeholder="请选择年份" size="small"
                                    style="width: 80px" @visible-change="getYears">
                                    <el-option v-for="item in yearsArr" :label="item" :value="item" :key="item" />
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                        <el-form-item label="班组:" size="small">
                            <el-config-provider :value-on-clear="''" :empty-values="['', undefined, null]">
                                <el-select v-model="paramsForm.persongroup" clearable placeholder="请选择班组" size="small"
                                    style="width: 150px" @visible-change="changePg">
                                    <el-option v-for="item in useStore.pgs" :label="item.PGNAME"
                                        :value="item.PERSONGROUP" :key="item.PERSONGROUP" />
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                        <el-form-item label="姓名:" size="small">
                            <el-input v-model="paramsForm.realname" />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" size="small" @click="getDatas(paramsForm)"
                                :loading="loading">查询</el-button>
                        </el-form-item>
                    </div>
                    <div class="two-box">
                        <el-form-item label="季度:" size="small">
                            <el-config-provider :value-on-clear="''" :empty-values="['', undefined, null]">
                                <el-select v-model="paramsForm.quarter" clearable placeholder="请选择季度" size="small"
                                    style="width: 100px" @visible-change="changeQuarter">
                                    <el-option v-for="item in quartersArr" :label="item.DESCRIPTION" :value="item.VALUE"
                                        :key="item.VALUE" />
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                        <el-form-item label="计划状态:" size="small">
                            <el-config-provider :value-on-clear="''" :empty-values="['', undefined, null]">
                                <el-select v-model="paramsForm.planStatus" clearable placeholder="请选择计划状态" size="small"
                                    style="width: 90px" @change="changePlanStatus">
                                    <el-option v-for="item in planStatusArr" :label="item.label" :value="item.value"
                                        :key="item.value" />
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                        <el-form-item label="发放状态:" size="small">
                            <el-config-provider :value-on-clear="''" :empty-values="['', undefined, null]">
                                <el-select v-model="paramsForm.outputStatus" clearable placeholder="请选择发放状态"
                                    size="small" style="width: 90px" @change="changeOutputStatus">
                                    <el-option v-for="item in outputStatusArr" :label="item.label" :value="item.value"
                                        :key="item.value" />
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                        <el-form-item label="签收状态:" size="small">
                            <el-config-provider :value-on-clear="''" :empty-values="['', undefined, null]">
                                <el-select v-model="paramsForm.signStatus" clearable placeholder="请选择签收状态" size="small"
                                    style="width: 90px" @change="changeSignStatus">
                                    <el-option v-for="item in signStatusArr" :label="item.label" :value="item.value"
                                        :key="item.value" />
                                </el-select>
                            </el-config-provider>
                        </el-form-item>
                    </div>
                </el-form>
            </div>
        </template>
        <template #control>
            <Control :batchIsShow="batchIsShow" :downloadBoxShow="downloadBoxShow" :permObj="permObj"
                @addClick="editDatas('add', {})" @showDownload="showDownloadBox"
                @downloadExcelPaging="downloadExcelPaging(newDatasArr, 'contentTable', '劳保计划信息')"
                @downloadExcelAll="downloadExcelAll(datasArr, '劳保计划信息')" @batchToggleShow="batchEdit"
                @batchDel="editSubmit('batchDel', selectRows)" @batchUpload="batchUpload"></Control>
            <Paging :dataLength="datasLength" :countPage="countPage" :btnDisabledObj="btnDisabledObj"
                v-model:firstPage="firstPage" v-model:pageSize="pageSize" @toNext="nextPage(firstPage)"
                @toLast="lastPage(firstPage)" @toFirst="toFirstPage" @toEnd="toEndPage" @toSpecify="toSpecifyPage"
                @changePsize="changePageSize"></Paging>
        </template>
        <template #content>
            <el-table :data="newDatasArr" style="width: 100%" v-loading="loading"
                @selection-change="handleSelectionChange" id="contentTable">
                <el-table-column type="selection" width="50" v-if="selectShow" />
                <el-table-column prop="ID" label="id" width="70" />
                <el-table-column prop="PERSONID" label="人员号" width="90" />
                <el-table-column prop="DISPLAYNAME" label="姓名" width="90" />
                <el-table-column prop="LBNUM" label="编码" width="150" />
                <el-table-column prop="DESCRIPTION" label="名称" width="250" />
                <el-table-column prop="QUNIT" label="单位" width="70" />
                <el-table-column prop="SERVICE_CYCLE" label="更换周期" width="80" />
                <el-table-column prop="QUANTITY" label="采购数量" width="80" />
                <el-table-column prop="PG_DESC" label="部门" width="150" />
                <el-table-column prop="NEEDNUM" label="需求申请号" width="150" />
                <el-table-column prop="LINE_DESC" label="线路" width="90" />
                <el-table-column prop="QUARTER" label="季度" width="70" />
                <el-table-column prop="YEAR" label="年份" width="70" />
                <el-table-column prop="CREATEDATE" label="导入日期" width="120">
                    <template #default="{ row }">
                        {{ dateFormat(row.CREATEDATE) }}
                    </template>
                </el-table-column>
                <el-table-column prop="PLAN_STATUS" label="计划状态" width="80" />
                <el-table-column prop="OUTPUTDATE" label="发放日期" width="120">
                    <template #default="{ row }">
                        {{ dateFormat(row.OUTPUTDATE) }}
                    </template>
                </el-table-column>
                <el-table-column prop="OUTPUTQUANTITY" label="发放数量" width="80" />
                <el-table-column prop="OUTPUT_STATUS" label="发放状态" width="80" />
                <el-table-column prop="SIGNDATE" label="签收日期" width="120">
                    <template #default="{ row }">
                        {{ dateFormat(row.SIGNDATE) }}
                    </template>
                </el-table-column>
                <el-table-column prop="SIGNQUANTITY" label="签收数量" width="80" />
                <el-table-column prop="SIGN_STATUS" label="签收状态" width="80" />
                <el-table-column prop="SIGNBY" label="签收人" width="90" />
                <el-table-column prop="REMARK" label="备注" width="150" />
                <el-table-column prop="PRODUCTDATE" label="生产日期" width="120">
                    <template #default="{ row }">
                        {{ dateFormat(row.PRODUCTDATE) }}
                    </template>
                </el-table-column>
                <el-table-column prop="SCHEDREDATE" label="计划发放日期" width="120">
                    <template #default="{ row }">
                        {{ dateFormat(row.SCHEDREDATE) }}
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="Operations" min-width="180" v-perm="permObj.update">
                    <template #default="{ row }">
                        <el-button type="primary" size="small" @click="editDatas('update', row)" title="编辑"
                            v-perm="permObj.update">
                            <el-icon>
                                <Edit />
                            </el-icon>
                        </el-button>
                        <el-button type="warning" size="small" @click="editDatas('copy', row)" title="复制"
                            v-perm="permObj.add">
                            <el-icon>
                                <CopyDocument />
                            </el-icon>
                        </el-button>
                        <el-button type="danger" size="small" class="del" title="删除" @click="editSubmit('delete', row)"
                            v-perm="permObj.delete">
                            <el-icon>
                                <DeleteFilled />
                            </el-icon>
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </template>
    </AdminSkeleton>
    <Drawer ref="drawerRef" :title="title" :size="size" @submit="editSubmit(submitType, editForm)"
        @cancel-submit="resetForm">
        <el-form :model="editForm" ref="updateFormRef" :rules="rules">
            <el-form-item label="ID" :label-width="formLabelWidth" prop="ID">
                <el-input v-model="editForm.ID" autocomplete="off" disabled />
            </el-form-item>
            <el-form-item label="人员ID" :label-width="formLabelWidth" prop="PERSONID">
                <el-input v-model="editForm.PERSONID" autocomplete="off" />
            </el-form-item>
            <el-form-item label="姓名" :label-width="formLabelWidth" prop="DISPLAYNAME">
                <el-input v-model="editForm.DISPLAYNAME" autocomplete="off" />
            </el-form-item>
            <el-form-item label="物资编码" :label-width="formLabelWidth" prop="LBNUM">
                <el-input v-model="editForm.LBNUM" autocomplete="off" />
            </el-form-item>
            <el-form-item label="名称" :label-width="formLabelWidth" prop="DESCRIPTION">
                <el-input v-model="editForm.DESCRIPTION" autocomplete="off" />
            </el-form-item>
            <el-form-item label="单位" :label-width="formLabelWidth" prop="QUNIT">
                <el-input v-model="editForm.QUINT" autocomplete="off" />
            </el-form-item>
            <el-form-item label="更换周期" :label-width="formLabelWidth" prop="SERVICE_CYCLE">
                <el-input v-model="editForm.SERVICE_CYCLE" autocomplete="off" />
            </el-form-item>
            <el-form-item label="采购数量" :label-width="formLabelWidth" prop="QUANTITY">
                <el-input v-model="editForm.QUANTITY" autocomplete="off" />
            </el-form-item>
            <el-form-item label="部门" :label-width="formLabelWidth" prop="PG_DESC">
                <el-input v-model="editForm.PG_DESC" autocomplete="off" />
            </el-form-item>
            <el-form-item label="需求申请号" :label-width="formLabelWidth" prop="NEEDNUM">
                <el-input v-model="editForm.NEEDNUM" autocomplete="off" />
            </el-form-item>
            <el-form-item label="线路" :label-width="formLabelWidth" prop="LINE_DESC">
                <el-input v-model="editForm.LINE_DESC" autocomplete="off" />
            </el-form-item>
            <!-- <el-form-item label="季度" :label-width="formLabelWidth" prop="QUARTER">
                <el-input v-model="editForm.QUARTER" autocomplete="off" />
            </el-form-item> -->
            <el-form-item label="季度" :label-width="formLabelWidth" prop="QUARTER">
                <el-config-provider :value-on-clear="''" :empty-values="['', undefined, null]">
                    <el-select v-model="editForm.QUARTER" clearable placeholder="请选择季度" style="width: 150px"
                        @visible-change="changeQuarter">
                        <el-option v-for="item in quartersArr" :label="item.DESCRIPTION" :value="item.VALUE"
                                        :key="item.VALUE" />
                    </el-select>
                </el-config-provider>
            </el-form-item>
            <el-form-item label="年度" :label-width="formLabelWidth" prop="YEAR">
                <el-input v-model="editForm.YEAR" autocomplete="off" />
            </el-form-item>
            <el-form-item label="导入日期" :label-width="formLabelWidth" prop="CREATEDATE">
                <el-date-picker v-model="editForm.CREATEDATE" type="date" placeholder="请选择日期" format="YYYY/MM/DD"
                    value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="计划状态" :label-width="formLabelWidth" prop="PLAN_STATUS">
                <el-config-provider :value-on-clear="''" :empty-values="['', undefined, null]">
                    <el-select v-model="editForm.PLAN_STATUS" clearable placeholder="请选择计划状态" style="width: 150px"
                        @visible-change="changePlanStatus">
                        <el-option v-for="item in planStatusArr" :label="item.label" :value="item.value"
                                        :key="item.value" />
                    </el-select>
                </el-config-provider>
            </el-form-item>
            <el-form-item label="发放日期" :label-width="formLabelWidth" prop="OUTPUTDATE">
                <el-date-picker v-model="editForm.OUTPUTDATE" type="date" placeholder="请选择日期" format="YYYY/MM/DD"
                    value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="发放数量" :label-width="formLabelWidth" prop="OUTPUTQUANTITY">
                <el-input v-model="editForm.OUTPUTQUANTITY" autocomplete="off" />
            </el-form-item>
            <el-form-item label="发放状态" :label-width="formLabelWidth" prop="OUTPUT_STATUS">
                <el-config-provider :value-on-clear="''" :empty-values="['', undefined, null]">
                    <el-select v-model="editForm.OUTPUT_STATUS" clearable placeholder="请选择发放状态" style="width: 150px"
                        @visible-change="changeOutputStatus">
                        <el-option v-for="item in outputStatusArr" :label="item.label" :value="item.value"
                                        :key="item.value" />
                    </el-select>
                </el-config-provider>
            </el-form-item>
            <el-form-item label="签收日期" :label-width="formLabelWidth" prop="SIGNDATE">
                <el-date-picker v-model="editForm.SIGNDATE" type="date" placeholder="请选择日期" format="YYYY/MM/DD"
                    value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="签收数量" :label-width="formLabelWidth" prop="SIGNQUANTITY">
                <el-input v-model="editForm.SIGNQUANTITY" autocomplete="off" />
            </el-form-item>
            <el-form-item label="签收状态" :label-width="formLabelWidth" prop="SIGN_STATUS">
                <el-config-provider :value-on-clear="''" :empty-values="['', undefined, null]">
                    <el-select v-model="editForm.SIGN_STATUS" clearable placeholder="请选择签收状态" style="width: 150px"
                        @visible-change="changeSignStatus">
                        <el-option v-for="item in signStatusArr" :label="item.label" :value="item.value"
                                        :key="item.value" />
                    </el-select>
                </el-config-provider>
            </el-form-item>
            <el-form-item label="签收人" :label-width="formLabelWidth" prop="SIGNBY">
                <el-input v-model="editForm.SIGNBY" autocomplete="off" />
            </el-form-item>
            <el-form-item label="备注" :label-width="formLabelWidth" prop="REMARK">
                <el-input v-model="editForm.REMARK" autocomplete="off" />
            </el-form-item>
            <el-form-item label="生产日期" :label-width="formLabelWidth" prop="PRODUCTDATE">
                <el-date-picker v-model="editForm.PRODUCTDATE" type="date" placeholder="请选择日期" format="YYYY/MM/DD"
                    value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="计划发放日期" :label-width="formLabelWidth" prop="SCHEDREDATE">
                <el-date-picker v-model="editForm.SCHEDREDATE" type="date" placeholder="请选择日期" format="YYYY/MM/DD"
                    value-format="YYYY-MM-DD" />
            </el-form-item>
        </el-form>
    </Drawer>
    <Dialog ref="dialogRef" :uploadKeys="uploadKeys" @uploadFile="uploadFile" @closeDialog="closeDialog"
        @batchUpdate="editSubmit('batchUpdate', uploadTable)" @batchAdd="editSubmit('batchAdd', uploadTable)"
        @downloadTemplate="downloadTemplate('upload-table', '劳保计划导入模板')">
        <el-table :data="uploadTable" style="width: 100%" ref="dialogTableRef" id="upload-table">
            <!-- <el-table-column type="selection" width="50" v-if="selectShow"/> -->
            <el-table-column fixed prop="ID" label="ID" width="80" />
            <el-table-column fixed prop="PERSONID" label="人员ID" width="80" />
            <el-table-column fixed prop="DISPLAYNAME" label="姓名" width="80" />
            <el-table-column fixed prop="LBNUM" label="物资编码" width="150" />
            <el-table-column prop="DESCRIPTION" label="名称" width="150" />
            <el-table-column prop="QUINT" label="单位" width="120" />
            <el-table-column prop="SERVICE_CYCLE" label="服务周期" width="120" />
            <el-table-column prop="QUANTITY" label="数量" width="120" />
            <el-table-column prop="PG_DESC" label="部门" width="120" />
            <el-table-column prop="NEEDNUM" label="需求申请号" width="150" />
                <el-table-column prop="LINE_DESC" label="线路" width="90" />
                <el-table-column prop="QUARTER" label="季度" width="70" />
                <el-table-column prop="YEAR" label="年份" width="70" />
                <el-table-column prop="CREATEDATE" label="导入日期" width="120">
                    <template #default="{ row }">
                        {{ dateFormat(row.CREATEDATE) }}
                    </template>
                </el-table-column>
                <el-table-column prop="PLAN_STATUS" label="计划状态" width="80" />
                <el-table-column prop="OUTPUTDATE" label="发放日期" width="120">
                    <template #default="{ row }">
                        {{ dateFormat(row.OUTPUTDATE) }}
                    </template>
                </el-table-column>
                <el-table-column prop="OUTPUTQUANTITY" label="发放数量" width="80" />
                <el-table-column prop="OUTPUT_STATUS" label="发放状态" width="80" />
                <el-table-column prop="SIGNDATE" label="签收日期" width="120">
                    <template #default="{ row }">
                        {{ dateFormat(row.SIGNDATE) }}
                    </template>
                </el-table-column>
                <el-table-column prop="SIGNQUANTITY" label="签收数量" width="80" />
                <el-table-column prop="SIGN_STATUS" label="签收状态" width="80" />
                <el-table-column prop="SIGNBY" label="签收人" width="90" />
                <el-table-column prop="REMARK" label="备注" width="150" />
                <el-table-column prop="PRODUCTDATE" label="生产日期" width="120">
                    <template #default="{ row }">
                        {{ dateFormat(row.PRODUCTDATE) }}
                    </template>
                </el-table-column>
                <el-table-column prop="SCHEDREDATE" label="计划发放日期" width="120">
                    <template #default="{ row }">
                        {{ dateFormat(row.SCHEDREDATE) }}
                    </template>
                </el-table-column>
            
        </el-table>
    </Dialog>
</template>

<script setup>
import { ref, reactive } from 'vue'
import AdminSkeleton from '@/components/AdminSkeleton.vue';
import Paging from '@/components/Paging.vue'
import Control from '@/components/Control.vue'
import Dialog from '@/components/Dialog.vue'
import Drawer from '@/components/Drawer.vue'
import { getLbListInfo, editLbListInfo, editManyLbListInfo } from '@/utils/lpsApi/lb_list'
import { getLbPlanInfo, editLbPlanInfo, editManyLbPlanInfo } from '@/utils/lpsApi/lb_plan'
import { getLbValuesInfo } from '@/utils/lpsApi/lb_values'
import { getPg } from '@/utils/adminApi/usrs'
import { paging } from '@/utils/paging'
import { exportClickFromTableId, exportClickFromDatas } from '@/utils/downloadExcel'
import { objCopy } from '@/utils/objCopy'
import { msgOpen } from '@/utils/msg'
import { confirm, confirmBatch } from '@/utils/confirm'
import { lpsStore } from '@/stores/lpsStore';
import { mainStore } from '@/stores/store'
import { dateFormat } from '@/utils/dateFormat'

const useLpsStore = lpsStore()
const useStore = mainStore()
// console.log(useLpsStore)

//权限对象--用于控制按钮显示
const componentName = 'Lb_list'
const permObj = {
    add: { name: componentName, type: 'add' },
    update: { name: componentName, type: 'update' },
    delete: { name: componentName, type: 'delete' }
}


//#region 查询***********************************************************************************************************************
const {
    datasArr,
    datasLength,
    firstPage,
    pageSize,
    countPage,
    newDatasArr,
    btnDisabledObj,
    pagingPreprocessing,
    nextPage,
    lastPage,
    toFirstPage,
    toEndPage,
    toSpecifyPage,
    changePageSize
} = paging() //分页


//查询参数表单对象
const paramsForm = reactive({
    planYear: "",
    quarter: "",
    planStatus: "",
    outputStatus: "",
    signStatus: "",
    persongroup: "",
    realname: ""
})

let yearsArr = ref([])  //年份
let quartersArr = ref([])  //季度
let planStatusArr = ref([{ label: "未完成", value: 0 }, { label: "已完成", value: 1 }])  //计划状态
let outputStatusArr = ref([{ label: "未发放", value: 0 }, { label: "已发放", value: 1 }])  //发放状态
let signStatusArr = ref([{ label: "未签收", value: 0 }, { label: "已签收", value: 1 }])  //签收状态


let pfFormRef = ref(null) //表单ref
//表单验证规则
const pfRules = reactive({
    planYear: [{ required: true, message: '请选择计划年份', trigger: ['blur'] }],
})

//获取年份
const getYears = () => {
    let fullYear = new Date().getFullYear() + 2
    let arr = []
    for (let i = 2020; i <= fullYear; i++) {
        arr.push(i)
    }
    yearsArr.value = arr
    // console.log(yearsArr.value)
}

//改变及获取班组信息
const changePg = async () => {
    if (!useStore.pgs.length) {
        let pgs = await getPg()
        useStore.setPgs(pgs.datas.pgs)
    }
}

//改变及获取季度
const changeQuarter = async () => {
    // console.log(useLpsStore.lbValInfo)
    if (!useLpsStore.lbValInfo.length) {
        let lbv = await getLbValuesInfo()
        lbv.status ? useLpsStore.setLbValInfo(lbv.resDatas.result) : []
        quartersArr.value = useLpsStore.lbValInfo.filter(item => item.CUST_TYPE === "LB_QUARTER")
    } else {
        quartersArr.value = useLpsStore.lbValInfo.filter(item => item.CUST_TYPE === "LB_QUARTER")
    }
}

//改变计划状态
const changePlanStatus = (val) => paramsForm.planStatus = val

//改变发放状态
const changeOutputStatus = (val) => paramsForm.outputStatus = val

//改变签收状态
const changeSignStatus = (val) => paramsForm.signStatus = val

// loading状态
let loading = ref(false);

//获取标签值列表
const getDatas = async (params) => {
    // console.log(params)
    // if (!params.planYear ===) {
    //     msgOpen("请选择计划年份")
    //     return
    // }
    pfFormRef.value.validate(async (valid) => {
        if (!valid) {
            return false
        } else {
            loading.value = true;
            const res = await getLbPlanInfo(params)
            console.log(res)
            pagingPreprocessing(res.resDatas.result)
            loading.value = false;
        }
    })
}
//#endregion********************************************************************************************************




//#region 编辑信息***************************************************************************************************
let title = ref("")
let size = ref("40%")
let formLabelWidth = ref("110")

//编辑用户信息
let drawerRef = ref(null) //组件ref
let oldLbValue = reactive({
    ID: '',
    PERSONID: '',
    DISPLAYNAME: '',
    LBNUM: '',
    DESCRIPTION: '',
    QUINT: '',
    SERVICE_CYCLE: '',
    QUANTITY: '',
    PG_DESC: '',
    NEEDNUM: '',
    LINE_DESC: '',
    QUARTER: '',
    YEAR: '',
    CREATEDATE: '',
    PLAN_STATUS: '',
    OUTPUTDATE: '',
    OUTPUTQUANTITY: '',
    OUTPUT_STATUS: '',
    SIGNDATE: '',
    SIGNQUANTITY: '',
    SIGN_STATUS: '',
    SIGNBY: '',
    REMARK: '',
    PRODUCTDATE: '',
    SCHEDREDATE: '',
})
let editForm = reactive({
    ID: '',
    PERSONID: '',
    DISPLAYNAME: '',
    LBNUM: '',
    DESCRIPTION: '',
    QUINT: '',
    SERVICE_CYCLE: '',
    QUANTITY: '',
    PG_DESC: '',
    NEEDNUM: '',
    LINE_DESC: '',
    QUARTER: '',
    YEAR: '',
    CREATEDATE: '',
    PLAN_STATUS: '',
    OUTPUTDATE: '',
    OUTPUTQUANTITY: '',
    OUTPUT_STATUS: '',
    SIGNDATE: '',
    SIGNQUANTITY: '',
    SIGN_STATUS: '',
    SIGNBY: '',
    REMARK: '',
    PRODUCTDATE: '',
    SCHEDREDATE: '',
})
//提交类型
let submitType = ref('')
//表单验证ref
let updateFormRef = ref(null)
//输入框验证规则
let rules = reactive({
    PERSONID: [
        { required: true, message: '请输入人员ID', trigger: 'blur' }
    ],
    DISPLAYNAME: [
        { required: true, message: '请输入姓名', trigger: 'blur' }
    ],
    LBNUM: [
        { required: true, message: '请输入物资编码', trigger: 'blur' }
    ],
    DESCRIPTION: [
        { required: true, message: '请输入描述', trigger: 'blur' }
    ],
    QUINT: [
        { required: true, message: '请输入单位', trigger: 'blur' }
    ],
    SERVICE_CYCLE: [
        { required: true, message: '请输入服务周期', trigger: 'blur' }
    ],
    QUANTITY: [
        { required: true, message: '请采购数量', trigger: 'blur' }
    ],
    PG_DESC: [
        { required: true, message: '请输入部门', trigger: 'blur' }
    ],
    NEEDNUM: [
        { required: true, message: '请输入需求数量', trigger: 'blur' }
    ],
    LINE_DESC: [
        { required: true, message: '请输入线体', trigger: 'blur' }
    ],
    QUARTER: [
        { required: true, message: '请选择季度', trigger: 'change' }
    ],
    YEAR: [
        { required: true, message: '请选择年份', trigger: 'change' }
    ],
    CREATEDATE: [
        { required: true, message: '请选择创建时间', trigger: 'change' }
    ],
    PLAN_STATUS: [
        { required: true, message: '请选择计划状态', trigger: 'change' }
    ],
    OUTPUT_STATUS: [
        { required: true, message: '请选择发放状态', trigger: 'change' }
    ],
    SIGN_STATUS: [
        { required: true, message: '请选择签收状态', trigger: 'change' }
    ],
})

//重置表单
const resetForm = () => {
    if (submitType.value === 'update' || submitType.value === 'copy') {
        editForm = objCopy(oldLbValue, editForm)
    } else if (submitType.value === 'add') {
        editForm = reactive({
            ID: '',
            PERSONID: '',
            DISPLAYNAME: '',
            LBNUM: '',
            DESCRIPTION: '',
            QUINT: '',
            SERVICE_CYCLE: '',
            QUANTITY: '',
            PG_DESC: '',
            NEEDNUM: '',
            LINE_DESC: '',
            QUARTER: '',
            YEAR: '',
            CREATEDATE: '',
            PLAN_STATUS: '',
            OUTPUTDATE: '',
            OUTPUTQUANTITY: '',
            OUTPUT_STATUS: '',
            SIGNDATE: '',
            SIGNQUANTITY: '',
            SIGN_STATUS: '',
            SIGNBY: '',
            REMARK: '',
            PRODUCTDATE: '',
            SCHEDREDATE: '',
        })
    }
}

/**
 * 处理单个设备编辑、删除
 * @param type 操作类型，add：新增，update：编辑，del：删除
 * @param row 
 */
const editDatas = (type, row) => {
    // console.log(type)
    // console.log(row)
    submitType.value = type
    if (type === 'update') {
        drawerRef.value.open()
        title.value = "编辑"
        editForm = row
        editForm.CREATEDATE ? editForm.CREATEDATE = dateFormat(row.CREATEDATE) : ''
        editForm.OUTPUTDATE ? editForm.OUTPUTDATE = dateFormat(row.OUTPUTDATE) : ''
        editForm.SIGNDATE ? editForm.SIGNDATE = dateFormat(row.SIGNDATE) : ''
        editForm.PRODUCTDATE ? editForm.PRODUCTDATE = dateFormat(row.PRODUCTDATE) : ''
        editForm.SCHEDREDATE ? editForm.SCHEDREDATE = dateFormat(row.SCHEDREDATE) : ''
        //存储未修改前用户信息
        oldLbValue = objCopy(row, oldLbValue)
    }
    if (type === 'add') {
        drawerRef.value.open()
        title.value = "新增"
        resetForm()
    }
    if (type === "copy") {
        drawerRef.value.open()
        title.value = "复制"
        editForm = objCopy(row, editForm)
        editForm.ID = ""
        editForm.DISPLAYNAME += "_副本"
        editForm.CREATEDATE ? editForm.CREATEDATE = dateFormat(row.CREATEDATE) : ''
        editForm.OUTPUTDATE ? editForm.OUTPUTDATE = dateFormat(row.OUTPUTDATE) : ''
        editForm.SIGNDATE ? editForm.SIGNDATE = dateFormat(row.SIGNDATE) : ''
        editForm.PRODUCTDATE ? editForm.PRODUCTDATE = dateFormat(row.PRODUCTDATE) : ''
        editForm.SCHEDREDATE ? editForm.SCHEDREDATE = dateFormat(row.SCHEDREDATE) : ''
    }
}
//#endregion********************************************************************************************************




//#region 批量处理***************************************************************************************************
let batchIsShow = ref(false) //展示批量上传、批量删除状态
let selectShow = ref(false) //展示批量勾选状态
let selectRows = ref([]) //选中的设备行数据
let dialogRef = ref(null) //组件ref
let uploadKeys = ref([
    "ID",
    "PERSONID",
    "DISPLAYNAME",
    "LBNUM",
    "DESCRIPTION",
    "QUINT",
    "SERVICE_CYCLE",
    "QUANTITY",
    "PG_DESC",
    "NEEDNUM",
    "LINE_DESC",
    "QUARTER",
    "YEAR",
    "CREATEDATE",
    "PLAN_STATUS",
    "OUTPUTDATE",
    "OUTPUTQUANTITY",
    "OUTPUT_STATUS",
    "SIGNDATE",
    "SIGNQUANTITY",
    "SIGN_STATUS",
    "SIGNBY",
    "REMARK",
    "PRODUCTDATE",
    "SCHEDREDATE",
]) //上传的字段

const batchEdit = () => {
    if (newDatasArr.value.length) selectShow.value = !selectShow.value
    batchIsShow.value = !batchIsShow.value
}

/**
 * 表格选中行数据变化时触发的方法
 * @param val 选中的设备行数据
 */
const handleSelectionChange = (val) => {
    selectRows.value = val
}

//批量上传
let uploadTable = ref([])
const batchUpload = () => {
    // console.log('batchUpload');
    dialogRef.value.open()
}

//关闭
const closeDialog = () => {
    uploadTable = ref([])
    dialogRef.value.close()
}

//接收子组件传递的excel数据，渲染至界面
let dialogTableRef = ref(null)
const uploadFile = (e) => {
    // console.log(e.value)            
    uploadTable = e.value
}

//下载
let downloadBoxShow = ref(false) //展示下载框状态
const showDownloadBox = () => {
    downloadBoxShow.value = !downloadBoxShow.value
}
/** 
 * 下载当前页
 * @param {Array} data 导出的数据数组对象
 * @param {String} tableId table标签的ID
 * @param {String} fileName 需要命名的文件名
 */
const downloadExcelPaging = (data, tableId, fileName) => {
    data.length ? exportClickFromTableId(tableId, fileName) : msgOpen("无数据可供下载，请先查询数据")
}
/**
 * 下载全部
 * @param {Array} data 导出的数据数组对象(未进行分页)
 * @param {String} fileName 需要命名的文件名
 */
const downloadExcelAll = (data, fileName) => {
    if (data.length) {
        let newData = JSON.parse(JSON.stringify(data)) //深拷贝      
        exportClickFromDatas(newData, fileName)
    } else {
        msgOpen("无数据可供下载，请先查询数据")
    }
}

/**
 * 导入模版下载
 * @param tableId table标签的ID
 * @param fileName 需要命名的文件名
 */
const downloadTemplate = (tableId, fileName) => {
    // console.log(tableId)
    exportClickFromTableId(tableId, fileName)
}
//********************************************************************************************************************** */
//#endregion


/**
 * 处理值编辑、删除、批量操作
 * @param type 操作类型，batchDel：批量删除
 * @param data 选中的值数据
 * 
 */
const editSubmit = async (type, data) => {
    // console.log(type)
    // console.log(data)
    if (type === 'add' || type === 'update' || type === 'copy') {
        updateFormRef.value.validate(async (valid) => {
            if (!valid) {
                return false
            } else {
                let res = await editLbPlanInfo(type, data)
                // console.log(res)
                drawerRef.value.hideLoading()
                drawerRef.value.close()
                res.status ? msgOpen(res.msg, 'success') : msgOpen(res.msg)
                getDatas(paramsForm)
                resetForm()
            }
        })
    }
    if (type === 'delete') {
        let res = await confirm(data.LBNUM)
        if (res) {
            let res = await editLbPlanInfo(type, data)
            res.status ? msgOpen(res.msg, 'success') : msgOpen(res.msg)
            getDatas(paramsForm)
        }
    }
    if (type === 'batchAdd' || type === 'batchUpdate') {
        let rowsArr = ref([])
        let editBatchStatus = ref(true)
        for (let i = 0; i < data.length; i++) {
            // console.log(data[i]);
            if(type === 'batchUpdate' && !data[i].ID){
                msgOpen("更新值，请填写ID！")   
                editBatchStatus.value = false
                break
            }else if(type === 'batchAdd' && data[i].ID){
                msgOpen("新增值，ID请留空！")
                editBatchStatus.value = false
                break  
            }else {
                rowsArr.value.push(data[i])
            }
        }
        if (editBatchStatus.value) {
            let res = await editManyLbPlanInfo(type, rowsArr.value)
            // console.log(res)            
            dialogRef.value.close()
            uploadTable = ref([])
            if (!res.status) {
                msgOpen(res.msg)
            } else {
                res.resDatas.result.rowsAffected ? msgOpen(`${res.msg},共${res.resDatas.result.rowsAffected}条`, "success")
                    : msgOpen(res.msg)
            }
            getDatas(paramsForm)
        }
    }
    if (type === 'batchDel') {
        if (!data.length) {
            msgOpen('未进行勾选，请勾选！')
        } else {
            let isDel = await confirmBatch(data.length)
            if (isDel) {
                let vNumArr = []
                data?.forEach(item => {
                    let arr = []
                    arr.push(item.ID)
                    vNumArr.push(arr)
                })
                let res = await editManyLbPlanInfo(type, vNumArr)
                if (!res.status) {
                    msgOpen(res.msg)
                } else {
                    res.resDatas.result.rowsAffected ? msgOpen(`${res.msg},共${res.resDatas.result.rowsAffected}条`, "success")
                        : msgOpen(res.msg)
                }
                getDatas(paramsForm)
            }
        }
    }
}
//#endregion

</script>

<style lang="less" scoped>
.params-form {
    height: 100%;
    width: 100%;

    .el-form {
        height: 100%;
        width: 100%;

        .one-box,
        .two-box {
            height: 50%;
            width: 100%;
            display: flex;
            justify-content: flex-start;
            align-items: center;

            .el-form-item {
                margin-bottom: 0;
                margin-right: .3125rem;
            }
        }
    }
}

.el-table {
    height: 95%;
}
</style>